﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fluent;
using System.Windows.Media.Imaging;
using System.Windows.Data;
using System.Windows.Media;

namespace SkynetQuery
{
   public class MainWindowViewModel
    {
       Ribbon _ribbon;
       public MainWindowViewModel(Ribbon ribbon)
       {
           _ribbon = ribbon;
       }

       public void LoadRibbonItems(List<ToolTabItem> tabs)
       {
           foreach (ToolTabItem ite in tabs)
           {
               _ribbon.Tabs.Add(CreateTabItem(ite));
           }
       }

       private RibbonTabItem CreateTabItem(ToolTabItem item)
       {
           if (item == null)
               return null;
           RibbonTabItem tab = new RibbonTabItem();
           tab.Header = item.Title;
           if (item.GroupItems != null)
           {
               foreach (ToolGroupItem group in item.GroupItems)
               {
                   tab.Groups.Add(CreateGroup(group));
               }
           }
           if (item.Contextual != null)
           {
               RibbonContextualTabGroup tabgroup = new RibbonContextualTabGroup
               {
                   Header = item.Contextual.Title,
                   Background = item.Contextual.Background,
                   BorderBrush = item.Contextual.Background
               };
               tabgroup.Visibility = new Binding();
               _ribbon.ContextualGroups.Add(tabgroup);
               tab.Group = tabgroup;
           }
           return tab;
       }
       private RibbonGroupBox CreateGroup(ToolGroupItem group)
       {
           if (group == null)
               return null;
           RibbonGroupBox groupbox = new RibbonGroupBox();
           groupbox.Header = group.Title;
           if (group.Items != null)
           {
               foreach (ToolItem tool in group.Items)
               {
                   groupbox.Items.Add(CreateButton(tool));
               }
           }

         

           return groupbox;
       }
       private  Button CreateButton(ToolItem tool)
       {
           if (tool == null)
               return null;
           Button btn = new Button() {
               Header = tool.Title,
               Icon = GetImageSource(tool.Icon,16),
               LargeIcon = GetImageSource(tool.LargeIcon,32),
               ToolTip = CreateTip(tool.ToolTip)
           };
           if (!string.IsNullOrEmpty(tool.Size))
           {
               btn.SizeDefinition = tool.Size;
           }
           if (tool.QA != null)
           {
               _ribbon.QuickAccessItems.Add(new QuickAccessMenuItem()
               {
                   IsChecked = tool.QA.IsChecked,
                   CommandTarget = btn
               });
           }
           return btn;
       }
       /// <summary>
       /// 创建提示
       /// </summary>
       /// <param name="title"></param>
       /// <param name="text"></param>
       /// <returns></returns>
       private ScreenTip CreateTip(Tip tip)
       {
           if (tip == null)
               return null;
           return new ScreenTip() { 
               Title = tip.Title, 
               Text =tip.Content,
               IsRibbonAligned =false };
       }

       public BitmapImage GetImageSource(string itemPath,int size)
       {
           if (string.IsNullOrEmpty(itemPath))
               return null;
           if (itemPath.StartsWith(".."))
           {
               itemPath = itemPath.Substring(2).Replace("Images", "Images/Tree");
           }
           BitmapImage img = new BitmapImage();
           img.BeginInit();
           //img.DecodePixelHeight = size;
           //img.DecodePixelWidth = size;
 
           img.CacheOption = BitmapCacheOption.OnLoad;
           img.UriSource = new Uri("pack://application:,,,/QueryResource;Component" + itemPath, UriKind.Absolute);
           img.EndInit();
           img.Freeze();
           return img;
       }
    }

   public abstract class AbstractTool
   {
       public string Title { get; set; }
   }
   public class ToolTabItem : AbstractTool
   {
       public ContextualGroups Contextual { get; set; }
       public List<ToolGroupItem> GroupItems { get; set; }
   }
   public class ContextualGroups : AbstractTool
   {
       public Brush Background { get; set; }
   }
   public class ToolGroupItem : AbstractTool
   {
     
       public List<ToolItem> Items { get; set; }
   }
   public class ToolItem : AbstractTool
   {
       public string Icon { get; set; }
       public string LargeIcon { get; set; }
       
       public Tip ToolTip { get; set; }
       public QuickAccess QA { get; set; }

       public string Size { get; set; }
   }
   public class Tip : AbstractTool
   {
       public string Content { get; set; }
   }
   public class QuickAccess
   {
       public bool IsChecked { get; set; }

   }
}
